//
//  M1EditDetailsVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/13.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1EditDetailsVC.h"
#import "M1PersonalDataVC.h"
#import "M1ChooseMateVC.h"
#import "M1SegmengtView.h"

@interface M1EditDetailsVC ()

@property (nonatomic, strong) M1PersonalDataVC *m1_detailVC;

@property (nonatomic, strong) M1ChooseMateVC *m1_mateVC;

@property (nonatomic, strong) UIViewController *m1_currentVC;

@end

@implementation M1EditDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"编辑资料";
    
    [self m1_createRightBarButtonItem:@"保存" titleColor:m1_color(0x000000) action:@selector(m1_rightAction)];
    
    [self m1_createUI];

}

- (void)m1_rightAction{
    
}


- (void)m1_createUI{
    
    M1SegmengtView *m1_segment = [[M1SegmengtView alloc] initWithFrame:CGRectMake(0, STATUS_HEIGHT + 44, SCREENWIDTH, 65)];
    [self.view addSubview:m1_segment];
    
    [self addChildViewController:self.m1_detailVC];
    [self.view addSubview:self.m1_detailVC.view];
    self.m1_currentVC = self.m1_detailVC;
    
    m1_segment.m1_switchScopeBlock = ^(NSInteger selectedSegmentIndex) {
        if ((self.m1_currentVC == self.m1_detailVC && selectedSegmentIndex == 1)||(self.m1_currentVC == self.m1_mateVC && selectedSegmentIndex == 2)) {
            return;
        }else{
            if (selectedSegmentIndex == 1) {
                [self replaceController:self.m1_currentVC newController:self.m1_detailVC];
            }else{
                [self replaceController:self.m1_currentVC newController:self.m1_mateVC];
            }
        }
    };
}

- (M1PersonalDataVC *)m1_detailVC{
    if (!_m1_detailVC) {
        _m1_detailVC = [[M1PersonalDataVC alloc] init];
        _m1_detailVC.view.frame = CGRectMake(0, STATUS_HEIGHT + 44 + 65, SCREENWIDTH, SCREENHEIGHT);
    }
    return _m1_detailVC;
}

- (M1ChooseMateVC *)m1_mateVC{
    if (!_m1_mateVC) {
        _m1_mateVC = [[M1ChooseMateVC alloc] init];
        _m1_mateVC.view.frame = CGRectMake(0, STATUS_HEIGHT + 44 + 65, SCREENWIDTH, SCREENHEIGHT);
    }
    return _m1_mateVC;
}

- (void) m1_createRightBarButtonItem:(NSString *)title titleColor:(UIColor *)color action:(SEL)action{
    CGFloat width;
    if ([title length] > 2) {
        width = 70;
    }else{
        width = 40;
    }
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, width, 44);
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = m1_font(16);
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.m1_currentVC = newController;
        }else{
            
            self.m1_currentVC = oldController;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
